// 1.1引入创建服务的内置模块http
const http = require("http");
// 1.2创建服务
const app = http.createServer();
//1.3给本次服务添加一个端口号
app.listen(4000, function () {
  console.log("本次服务运行在4000端口上");
});

//1.4监听客户端访问请求
app.on("request", function (req, res) {
  // 获取客户端请求的路径
  let userUrl = req.url;
  // 根据客户端不同的请求路径做出不同的反馈结果处理
  if (userUrl == "/login") {
    res.writeHead(200, "success", { "content-type": "text/html;charset=utf8" });
    res.write("<h2>hello，恭喜你登录成功，你是第一个用户</h2>");
    res.end();
  } else if (userUrl == "/userList") {
    res.writeHead(200, "success", {
      "content-type": "application/json;charset=utf8",
    });
    let list = [
      {
        id: 11,
        name: "李四",
      },
      {
        id: 12,
        name: "张三",
      },
    ];
    let obj = {
      code: 200,
      msg: "请求成功",
      list,
    };
    res.end(JSON.stringify(obj));
  }
});
